$(function(){
	var pageNum = 1, pageSize = 10, dataCount = 0;
	let S_arr = [];
	var dataList = [];
	function requestData() {
		$.ajax({
       timeout:30000,
			url: window.Http + "/engine/apply/list",
			type: 'GET',
			headers: {
				'Authorization': "Bearer " + sessionStorage.getItem('Token')
			},
			data: { pageNum, pageSize,installerId:sessionStorage.getItem('UserId')},
			success: function (res) {
				if (res.code == 200) {
					$("#myConfigureTable tbody").empty();
					dataList = res.rows || [];
					if (res.rows && res.rows.length > 0) {
						let html = "";
						let statushtml = "";
						let state = "正常";
						let herfUrl = "";
						// let productPrice ; // 产品价格
						res.rows.forEach(function (val, index) {
							if (val.goodsType == 1) {
								herfUrl = "./productDetail.html?serverId=" + val.goodsId + "&goodsType=" + val.goodsType;
							} else {
								herfUrl = "./productDetail.html?directoryId=" + val.goodsId + "&goodsType=" + val.goodsType;
							}
							switch (val.status || 0) {
								case -1:
									state = "下架";
									statushtml = `<a class="layui-btn layui-btn-xs text-white Disable mr-2">启用</a>
												 <a class="layui-btn layui-btn-xs layui-btn-normal text-white configuration mr-2">配置信息</a>`
									break;
								case 0:
									state = "正常";
									statushtml = `<a class="layui-btn layui-btn-xs text-white Disable mr-2">启用</a>
												<a class="layui-btn layui-btn-xs layui-btn-normal text-white configuration mr-2">配置信息</a>`
									break;
								case 1:
									state = "上架";
									statushtml = `<a class="layui-btn layui-btn-xs layui-btn-danger text-white Disable mr-2">禁用</a>`
									break;
								case 2:
									state = "通过";
									break;
								case 3:
									state = "驳回";
									break;
								default:
									break;
							}
							html += `<tr dataId='${val.id}' dataStatus='${val.status}'>
									<th scope="row">`+ (index + 1) + `</th>
									<td>`+ val.engineApplyName + `</td>
									<td>`+ `${val.sourceName || '—'}` + `</td>
									<td>`+ state + `</td>
									<td>`+ val.installTime + `</td>
									<td>`+ `${val.remark ? val.remark: '—' }`+ `</td>
									<td>`+ statushtml +`</td>
								</tr>`
							S_arr.push(val.id)
						})
						$("#myConfigureTable tbody").append(html);
					} else {
						let html = `<tr><td scope="row" colspan="8" style="text-align:center">暂无数据</td></tr>`
						$("#myConfigureTable tbody").append(html);
					}
					dataCount = res.total || 0;
					if(pageNum == 1){
						layui.laypage.render({
							elem: 'myConfigureTablePage', //注意，这里的 test1 是 ID，不用加 # 号
							count: dataCount, //数据总数，从服务端得到
							limit: pageSize,
							jump: function (obj, first) {
								if(!first){
								  pageNum = obj.curr;
								  pageSize = obj.limit;
								  requestData();
								}
							}
						});
					}

				}

			}
		})
	}
	requestData();
	parame = {
		"auditResult": "",
		"id": 0,
		"operateType": 1,
		"status": -1
	  }
	// 禁用/启用 /engine/apply/operate
	$('#myConfigureTable tbody').delegate('.Disable','click',function(){
		let id = $(this).parents('[dataId]').attr('dataid')
		parame.id = +id
		if ($('.Disable').html() == '禁用'){
			parame.status = 1
			$(`[dataId="${id}"] .Disable`).html('启用')
			$(`[dataId="${id}"] .Disable`).removeClass('layui-btn-danger')
			$.ajax({
       timeout:30000,
				url: window.Http + "/engine/apply/operate",
				type: 'POST',
				headers: {
					'Authorization': "Bearer " + sessionStorage.getItem('Token')
				},
				contentType: 'application/json',
				data: JSON.stringify(parame),
				success: function (res) {
					requestData();
				}
			})
		} else {
			parame.status = -1
			$(`[dataId="${id}"] .Disable`).html('禁用')
			$(`[dataId="${id}"] .Disable`).addClass('layui-btn-danger')
			$.ajax({
       timeout:30000,
				url: window.Http + "/engine/apply/operate",
				type: 'POST',
				headers: {
					'Authorization': "Bearer " + sessionStorage.getItem('Token')
				},
				contentType: 'application/json',
				data: JSON.stringify(parame),
				success: function (res) {
					requestData();
				}
			})
		}
	})
	//查看引擎配置信息
	$('#myConfigureTable tbody').delegate('.configuration','click',function(){
		let id = $(this).parents('[dataId]').attr('dataid');
		let obj;
		dataList.forEach((value,index)=>{
			if(id == value.id){
				obj = value;
			}
		})
		layui.layer.open({
		  title: '提示',
		  content: obj.engineConfig,
		  area: '600px',

		});
	})
})
